import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class AjaxServiceService {
  domain: String = environment.baseUrl;

  isshowloading = true;
  constructor(private http: HttpClient, private router: Router, private message: NzMessageService) {

  }
  get(api: string, param: any) {
    if (this.isshowloading) {

    }
    return new Promise((resolve, reject) => {

      this.http.get(this.domain + api, {
        headers: new HttpHeaders().set('Content-Type', 'application/json').set("Authorization", localStorage.getItem("Authorization") ?? ""),
        params: param
      }).subscribe((response: any) => {
        if (this.isshowloading) {

        }
        resolve(response);

      }, (error: any) => {
        console.log(error);
        if (error.status == 401) {
          this.router.navigate(["/login"]);
          this.message.error("登录超时");
          return;
        }
      })
    })

  };
  post(api: string, data: any) {
    console.log(1);
    if (this.isshowloading) {

    }
    return new Promise((resolve, reject) => {

      this.http.post(this.domain + api, data, {
        headers: new HttpHeaders().set('Content-Type', 'application/json').set("Authorization", localStorage.getItem("Authorization") ?? ""),
      }).subscribe((response: any) => {
        if (this.isshowloading) {
        }
        resolve(response);
      }, (error: any) => {
        console.log(error);
        if (error.status == 401) {
          this.router.navigate(["/login"]);
          this.message.error("登录超时");
          return;
        }
      })

    })
  }

}
